package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.adapter.ChooseBikeCouponAdapter;
import com.ekuaitu.kuaitu.adapter.ChooseCouponAdapter;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.base.BaseRcQuickAdapter;
import com.ekuaitu.kuaitu.bean.SettlementBikeDetailBean;
import com.ekuaitu.kuaitu.bean.SettlementDetailBean;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChooseCouponActivity extends BaseActivity {

    @BindView(R.id.recyclerView_choose_coupon)
    RecyclerView recyclerViewChooseCoupon;

    private Context mContext = this;
    private ChooseCouponAdapter carAdapter;
    private ChooseBikeCouponAdapter bikeAdapter;
    private String orderId;
    private String bikeId;
    private String tripMode;

    @Override
    public int getLayoutId() {
        return R.layout.activity_choose_coupon;
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        tripMode = intent.getStringExtra(IntentKey.INTENT_TRIP_MODE);
        if (tripMode.equals("1")) {
            orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
            getSettlementDetail();
            final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            recyclerViewChooseCoupon.setLayoutManager(linearLayoutManager);
            recyclerViewChooseCoupon.setItemAnimator(new DefaultItemAnimator());
            carAdapter = new ChooseCouponAdapter(mContext, R.layout.item_my_coupon);
            recyclerViewChooseCoupon.setAdapter(carAdapter);
            carAdapter.setOnItemClickListener(new BaseRcQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent();
                    intent.putExtra("couponPrice", carAdapter.getItem(position).getDenomination() + "");
                    intent.putExtra("couponId", carAdapter.getItem(position).getCouponId() + "");
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, carAdapter.getItem(position).getTitle() + "");
                    setResult(Constants.RESULT_CHOOSE_COUPON, intent);
                    finish();
                }
            });
        } else if (tripMode.equals("2")) {
            orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
            bikeId = intent.getStringExtra(IntentKey.INTENT_BIKE_ID);
            getSettlementBikeDetail();
            final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            recyclerViewChooseCoupon.setLayoutManager(linearLayoutManager);
            recyclerViewChooseCoupon.setItemAnimator(new DefaultItemAnimator());
            bikeAdapter = new ChooseBikeCouponAdapter(mContext, R.layout.item_bike_coupon_list);
            recyclerViewChooseCoupon.setAdapter(bikeAdapter);
            bikeAdapter.setOnItemClickListener(new BaseRcQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent();
                    intent.putExtra("couponPrice", bikeAdapter.getItem(position).getDenomination() + "");
                    intent.putExtra("couponId", bikeAdapter.getItem(position).getCouponId() + "");
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, bikeAdapter.getItem(position).getTitle() + "");
                    setResult(Constants.RESULT_CHOOSE_COUPON, intent);
                    finish();
                }
            });
        }
    }


    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0, true);
    }

    private void getSettlementDetail() {

        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).settlementDetail(MyApplication.getInstance().getRequestFieldMap(), orderId).enqueue(new Callback<SettlementDetailBean>() {
            @Override
            public void onResponse(Call<SettlementDetailBean> call, Response<SettlementDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        SettlementDetailBean.AttachmentBean result = response.body().getAttachment();
                        carAdapter.replaceAll(result.getCoupons());
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<SettlementDetailBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getSettlementBikeDetail() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).settlementBikeDetail(MyApplication.getInstance().getRequestFieldMap(), orderId, bikeId).enqueue(new Callback<SettlementBikeDetailBean>() {
            @Override
            public void onResponse(Call<SettlementBikeDetailBean> call, Response<SettlementBikeDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        SettlementBikeDetailBean.AttachmentBean result = response.body().getAttachment();
                        bikeAdapter.replaceAll(result.getModel().getCoupons());
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<SettlementBikeDetailBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick({R.id.return_choose_coupon, R.id.button_choose_coupon_no})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.return_choose_coupon:
                finish();
                break;
            case R.id.button_choose_coupon_no:
                Intent intent = new Intent();
                intent.putExtra("couponPrice", 0 + "");
                intent.putExtra("couponId", -1 + "");
                setResult(Constants.RESULT_CHOOSE_COUPON, intent);
                finish();
                break;
        }
    }
}
